package mymac.com.test_3_1;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mymac on 2017/2/24.
 */

public class HttpActivity {

    private static HttpActivity httpActivity;

    public static HttpActivity getInstance() {
        if (httpActivity == null) {
            httpActivity = new HttpActivity();
        }
        return httpActivity;
    }

    public static void httpRequest(final String address, final Handler handler) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = Message.obtain();
                try {
                    URL url = new URL(address);
                    Log.e("------url:", url.toString());
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer stringBuffer = new StringBuffer();
                        String readLine = "";
                        while ((readLine = bufferedReader.readLine()) != null) {
                            stringBuffer.append(readLine);
                        }
                        bufferedReader.close();
                        inputStream.close();

                        String result = stringBuffer.toString();
                        JSONObject json = new JSONObject(result);
                        System.out.println(json.toString());
                        msg.obj = json;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //获取msg
    public static void getErrMsg(final String address, final DataCallBack dataCallBack) {
        HttpActivity.getInstance().httpRequest(address, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                JSONObject json = (JSONObject) msg.obj;
                String errmsg = json.optString("errmsg");
                dataCallBack.onErr(errmsg);
            }
        });
    }

    //获取json
    public static void getJson(String address, final DataCallBack dataCallBack){
        HttpActivity.getInstance().httpRequest(address, new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                JSONObject json = (JSONObject) msg.obj;
                dataCallBack.onErr(json);
            }
        });
    }
}
